﻿using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
using Box2DX.Common;
using Box2DX.Dynamics;

namespace Expression.Samples.Physics {

	// Used for debugging the physics engine, will render what the engine is seeing (rather than what's on the screen).
	// still really buggy though and draws at the wrong scale :(
#if DEBUG
	public class DebugDrawer: DebugDraw {

		private Canvas canvas;
		private List<FrameworkElement> elements = new List<FrameworkElement>();
		private SimulatorContext context;

		public DebugDrawer(Canvas canvas, SimulatorContext context) {
			this.canvas = canvas;
			this.context = context;

			this.Flags = DrawFlags.Shape;

		}


		public override void DrawPolygon(Vector2[] vertices, int vertexCount, Box2DX.Dynamics.Color color) {

			if (vertexCount > 2) {
				PathFigure figure = new PathFigure();
				figure.StartPoint = this.context.WorldToScreen(vertices[0]);

				for (int i = 1; i < vertexCount; ++i) {
					figure.Segments.Add(new LineSegment() {
						Point = this.context.WorldToScreen(vertices[i]),
					});
				}

				PathGeometry geometry = new PathGeometry();
				geometry.Figures.Add(figure);

				Path path = new Path();
				path.Data = geometry;

				path.StrokeThickness = 1;
				path.Stroke = new SolidColorBrush(color.ToPlatformColor());

				this.elements.Add(path);
				this.canvas.Children.Add(path);

			}
			
		}

		public override void DrawSolidPolygon(Box2DX.Common.Vector2[] vertices, int vertexCount, Box2DX.Dynamics.Color color) {
			if (vertexCount > 2) {
				PathFigure figure = new PathFigure();
				figure.StartPoint = this.context.WorldToScreen(vertices[0]);

				for (int i = 1; i < vertexCount; ++i) {
					figure.Segments.Add(new LineSegment() {
						Point = this.context.WorldToScreen(vertices[i]),
					});
				}

				PathGeometry geometry = new PathGeometry();
				geometry.Figures.Add(figure);

				Path path = new Path();
				path.Data = geometry;

				path.Fill = new SolidColorBrush(color.ToPlatformColor());

				this.elements.Add(path);
				this.canvas.Children.Add(path);

			}
		}

		public override void DrawCircle(Box2DX.Common.Vector2 center, float radius, Box2DX.Dynamics.Color color) {

			Ellipse ellipse = new Ellipse();
			Point size = this.context.WorldToScreen(new Vector2(radius, radius));

			ellipse.Width = size.X * 2;
			ellipse.Height = size.Y * 2;

			Point centerPoint = this.context.WorldToScreen(center);
			Canvas.SetLeft(ellipse, centerPoint.X - ellipse.Width / 2);
			Canvas.SetTop(ellipse, centerPoint.Y - ellipse.Height / 2);

			ellipse.StrokeThickness = 1;
			ellipse.Stroke = new SolidColorBrush(color.ToPlatformColor());

			this.elements.Add(ellipse);
			this.canvas.Children.Add(ellipse);
		}

		public override void DrawSolidCircle(Box2DX.Common.Vector2 center, float radius, Box2DX.Common.Vector2 axis, Box2DX.Dynamics.Color color) {

			Ellipse ellipse = new Ellipse();
			Point size = this.context.WorldToScreen(new Vector2(radius, radius));

			ellipse.Width = size.X * 2;
			ellipse.Height = size.Y * 2;

			Point centerPoint = this.context.WorldToScreen(center);
			Canvas.SetLeft(ellipse, centerPoint.X - ellipse.Width / 2);
			Canvas.SetTop(ellipse, centerPoint.Y - ellipse.Height / 2);

			ellipse.Fill = new SolidColorBrush(color.ToPlatformColor());

			this.elements.Add(ellipse);
			this.canvas.Children.Add(ellipse);
		}

		public override void DrawSegment(Box2DX.Common.Vector2 p1, Box2DX.Common.Vector2 p2, Box2DX.Dynamics.Color color) {
			throw new System.NotImplementedException();
		}

		public override void DrawXForm(Box2DX.Common.XForm xf) {
			throw new System.NotImplementedException();
		}
	}
#endif
}
